﻿#region [ License information          ]
/* ************************************************************
 *
 * Copyright (c) Bjørn Bouet Smith, 2012
 *
 * This source code is subject to terms and conditions of 
 * Microsoft Public License (Ms-PL).
 * 
 * A copy of the license can be found in the license.txt
 * file at the root of this distribution. If you can not 
 * locate the License, please send an email to bjornsmith@gmail.com
 * 
 * By using this source code in any fashion, you are 
 * agreeing to be bound by the terms of the Microsoft 
 * Public License.
 *
 * You must not remove this notice, or any other, from this
 * software.
 *
 * ************************************************************/
#endregion

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Smith.BuildExtensions.Logging
{
    /// <summary>
    /// Class that writes log statements to the underlying TaskLoggingHelper
    /// </summary>
    public class LogWriter : ILogWriter
    {
        private readonly TaskLoggingHelper logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="LogWriter"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        public LogWriter(TaskLoggingHelper logger)
        {
            this.logger = logger;
        }

        /// <summary>
        /// Logs the message to the underlying logger
        /// </summary>
        /// <param name="message">The message to log.</param>
        public void LogMessage(Message message)
        {
            switch (message.Level)
            {
                case MessageLevel.Error:
                    logger.LogError(message.MessageString);
                    return;
                case MessageLevel.Warn:
                    logger.LogWarning(message.MessageString);
                    return;
                case MessageLevel.Info:
                    logger.LogMessage(MessageImportance.High, message.MessageString);
                    return;
            }
        }
    }
}
